package activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lanou.horo.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import utils.adapter.GuideViewPagerAdapter;

/**
 * Created by dllo on 15/11/23.
 */
public class GuideActivity extends Activity implements View.OnClickListener,ViewPager.OnPageChangeListener {
    private ViewPager vp;
    private GuideViewPagerAdapter vpAdapter;
    private List<View> views;
    private Button startbutton;

    //引导图片资源
    private static final int[] pics = {
            R.mipmap.landing_page1,
            R.mipmap.landing_page2,
            R.mipmap.landing_page3 ,
    };

    //底部小店图片
    private ImageView[] dots ;

    //记录当前选中位置
    private int currentIndex;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.welcome);
        startbutton=(Button)findViewById(R.id.startbutton);
        views = new ArrayList<View>();

        LinearLayout.LayoutParams mParams = new
                LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        //初始化引导图片列
        for(int i=0; i<pics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[i]);
            views.add(iv);
        }
        vp = (ViewPager) findViewById(R.id.welcomeviewpager);
        //初始化Adapter
        vpAdapter = new GuideViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        //绑定回调
        vp.setOnPageChangeListener(this);
        startbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent=new Intent();
                intent.setClass(GuideActivity.this, MainActivity.class);
                GuideActivity.this.startActivity(intent);
                finish();
            }
        });

    }



    /**
     *设置当前的引导页
     */
    private void setCurView(int position)
    {
        if (position < 0 || position >= pics.length) {
            return;
        }

        vp.setCurrentItem(position);
    }


    //当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    //当当前页面被滑动时调�?
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    //当新的页面被选中时调�?
    @Override
    public void onPageSelected(int arg0) {
        //设置底部小点选中状�?
        if(arg0 == 2){
            startbutton.setVisibility(View.VISIBLE);

        }else{
            startbutton.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (Integer)v.getTag();
        setCurView(position);
    }
}
